//
//  SEGTrackMatrelsController.m
//  Uhomebk
//
//  Created by mac on 2017/11/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGTrackMatrelsController.h"
#import "SEGOrderSeiverListCell.h"
#import "SEGOrderPayItemModel.h"
#import "SEGOrderTrackInfoModel.h"

static  NSString *const kCellIdentifier = @"SEGTrackMaterelCell";

@interface SEGTrackMatrelsController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong) NSArray<SEGOrderPayItemModel *>* payItemModels;

@end

@implementation SEGTrackMatrelsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self handleData];
}

- (void)setupUI {
    self.title = @"物料清单";
    [self showCustomNavigationBackButton];
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = kColor_bg;
        tableView.tableFooterView = [[UIView alloc] init];
        [tableView registerClass:[SEGOrderSeiverListCell class] forCellReuseIdentifier:kCellIdentifier];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.estimatedRowHeight = 40.0f;
        tableView.rowHeight = UITableViewAutomaticDimension;
        [self.view addSubview:tableView];
        tableView;
    });
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
}

- (void)handleData {
    NSMutableArray<SEGOrderPayItemModel *> *tempData = [[NSMutableArray alloc] init];
    for (SEGOrderMateriels *materielsModel in self.dataArray) {
        SEGOrderPayItemModel *itemModel = [[SEGOrderPayItemModel alloc] init];
        itemModel.catalogName = materielsModel.mDictionary[@"catalogName"];
        itemModel.itemName = materielsModel.goodsName;
        itemModel.itemModel = materielsModel.goodsModel;
        itemModel.itemBrand = materielsModel.goodsBrand;
        itemModel.useCount = materielsModel.mDictionary[@"useCount"];
        itemModel.price = materielsModel.price;
        itemModel.unit = materielsModel.mDictionary[@"unit"];
        itemModel.type = [materielsModel.mDictionary[@"type"] integerValue];
        itemModel.verificationType = [materielsModel.mDictionary[@"verificationType"] integerValue];
        [tempData addObject:itemModel];
    }
    self.payItemModels = [tempData copy];
    self.tableView.tableFooterView = [self totalFeeView:[self calculateTotalPrice]];
    [self.tableView reloadData];
}

- (CGFloat)calculateTotalPrice {
    CGFloat result = 0.0;
    for (SEGOrderPayItemModel *item in self.payItemModels) {
        result += [item subtotalPrice];
    }
    return result;
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGOrderSeiverListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    SEGOrderPayItemModel *itemModel = self.payItemModels[indexPath.row];
    cell.row = indexPath.row;
    [cell updateWithModel:itemModel];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - view
- (UIView *)totalFeeView:(CGFloat)totalFee {
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 88)];
    footer.backgroundColor = [UIColor clearColor];
    
    UIView *labelBox = [[UIView alloc] init];
    labelBox.backgroundColor = kColor_W;
    [footer addSubview:labelBox];
    [labelBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(footer);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *totalPriceLabel = [[UILabel alloc] init];
    totalPriceLabel.backgroundColor = kColor_W;
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:@"总计："];
    [attributedString addAttributes:@{
                                      NSFontAttributeName : kFont_17,
                                      NSForegroundColorAttributeName : kColor_1
                                      }
                              range:NSMakeRange(0, attributedString.length)];
    NSMutableAttributedString *priceString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f元", totalFee]];
    [priceString addAttributes:@{
                                 NSFontAttributeName : [UIFont boldSystemFontOfSize:17],
                                 NSForegroundColorAttributeName : kColor_S1
                                 }
                         range:NSMakeRange(0, priceString.length)];
    [attributedString appendAttributedString:priceString];
    totalPriceLabel.attributedText = attributedString;
    [labelBox addSubview:totalPriceLabel];
    [totalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(labelBox).offset(-15);
        make.top.bottom.equalTo(labelBox);
    }];
    
    UIImageView *decorateImageView = [[UIImageView alloc] init];
    decorateImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"bg_border")];
    decorateImageView.contentMode = UIViewContentModeScaleAspectFill;
    [footer addSubview:decorateImageView];
    [decorateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelBox.mas_bottom);
        make.left.right.equalTo(footer);
        make.height.mas_equalTo(4);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColor_L2;
    [footer addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(footer);
        make.height.mas_equalTo(1);
    }];
    
    return footer;
}

@end
